package com.joey.cloud.communal.utils;

import org.springframework.cglib.beans.BeanCopier;

import java.util.ArrayList;
import java.util.List;

/**
 * 对象复制工具类
 * @author joey
 */
public class JoeBeanCopyUtil<T,V>{

    public static JoeBeanCopyUtil create(){
        return new JoeBeanCopyUtil();
    }

    public List<V> copyList(List<T> list, Class source, Class target){
        if(list==null||list.size()==0){
            return new ArrayList<V>();
        }
        try {
            List<V> result = new ArrayList<>(list.size());
            BeanCopier beanCopier = BeanCopier.create(source, target, false);
            for(T t : list){
                V v = (V)target.newInstance();
                beanCopier.copy(t,v,null);
                result.add(v);
            }
            return result;
        }catch (Exception e){
            e.printStackTrace();
        }
        return new ArrayList<V>();
    }

}
